<?php

namespace Web\MainSiteBundle\Controller;

use Model\CourseBundle\Entity\Course;
use Model\CourseBundle\Entity\CourseLessonArrange;
use Model\CourseBundle\Model\CourseLessonArrangeModel;
use Model\CourseBundle\Model\CourseLessonContentModel;
use Model\CourseBundle\Model\CourseModel;
use Model\CourseCategoryBundle\Model\CourseCategoryModel;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;

/**
 * @route("course")
 */
class ManageCourseLessonController extends WebBaseController
{
    /**
     * @route("/lesson/add/{uniqueId}",name="WebCourseLessonContentAdd")
     */
    public function WebCourseLessonContentAdd (Request $request , Course $course)
    {
        $this->needToBeCourseManager($course);
        $courseId = $course->getId();
        $courseLessonArrangeModel = new CourseLessonArrangeModel($this->getDoctrine());
        $courseLessonContentModel = new CourseLessonContentModel($this->getDoctrine());
        if ($request->getMethod() == "POST") {
            if (!$arrangeId = $this->getParamRequired("arrangeId")) {$this->showMessagePage("请选择课时章节");}
            if (!$title = $this->getParamRequired("title")) {$this->showMessagePage("课时标题不能为空");}
            if (!$orders = $this->getParamRequired("orders")) {$orders=0;}
            if (!$info = $this->getParamRequired("info")) {$this->showMessagePage("请填写课时简介");}
            if (!$type = $this->getParamRequired("type")) {$type=0;}//0=图文,1=视频,2=音频
            if (!in_array($type,array(0,1,2))) {$this->showMessagePage("请选择正确的课时类型");}
            if (!$content = $this->getParamRequired("content")) {$this->showMessagePage("请填写课时内容");}//0=图文内容,1=视频地址,2=音频地址
            $params = array(
                "title"     => $title,
                "courseId"  => $courseId,
                "arrangeId" => $arrangeId,
                "orders"    => $orders,
                "name"      => $title,
                "info"      => $info,
                "type"      => $type,
                "content"   => $content,
            );
            if ($id = $this->getParamRequired("id")) {
                $params["id"] = $id;
            }
            $rst = $courseLessonContentModel->commWebAdd($params);
            if (! $rst) {
                $this->showMessagePage("系统错误请刷新后重试，如果错误仍然存在，请与管理员联系！");
                return "unreachable!";
            } else {
                return $this->redirectToRoute("WebCourseLessonList",array("uniqueId"=>$course->getUniqueId()));
            }
        } else {
            $id = $this->getParamRequired("id");
            $courseLessonArranges = $courseLessonArrangeModel->getAllCourseLessonArrangeTree($courseId);
            $data["courseLessonArranges"] = $courseLessonArranges;
            if ($courseLessonArrangeId = $this->getParamRequired("arrange")) {
                $courseLessonArrange = $courseLessonArrangeModel->getOneArray($courseLessonArrangeId,$course->getId());
            } else {
                $courseLessonArrange = array();
            }

            $data["courseLessonArrange"] = $courseLessonArrange;
            $data["course"] = $course;
            if ($id) {//编辑
                $entity = $courseLessonContentModel->getOneArray($id,$courseId);
                $data["entity"] = $entity;
            } else {//新增

            }
            return $this->render("WebMainSiteBundle:Course:course_lesson_content_add.html.twig",$data);
        }
    }

    /**
     * @route("/lesson/list/{uniqueId}",name="WebCourseLessonList")
     */
    public function WebCourseLessonList (Request $request , Course $course)
    {
        $this->needToBeCourseManager($course);
        $courseId = $course->getId();
        $courseLessonArrangeModel = new CourseLessonArrangeModel($this->getDoctrine());
        $courseLessonContentModel = new CourseLessonContentModel($this->getDoctrine());

        $arrange = $courseLessonArrangeModel->getAllCourseLessonArrangeTree($courseId);
        foreach ($arrange as $k => &$v) {
            $lessons = $courseLessonContentModel->getListForCourseArrange($v["id"],$courseId);
            $v["lessons"] = $lessons;
        }
        $data = array(
            "course"    => $course,
            "courseLessonArrange"=>$arrange
        );
        return $this->render("WebMainSiteBundle:Course:course_lesson_list.html.twig",$data);
    }
}
